﻿using System;
using System.ComponentModel;
using System.Diagnostics;

namespace GVPSys.sysbase
{
    /// <summary>
    /// WPF界面与逻辑实现双向绑定基础base类
    /// </summary>
    [Serializable]
    public abstract class ProperChange : INotifyPropertyChanged
    {
        #region Debug
        [Conditional("DEBUG")]
        [DebuggerStepThrough]
        public void VerifyPropertyName(string propertyName)
        {

            if (TypeDescriptor.GetProperties(this)[propertyName] == null)
            {
                string msg = "Invalid property viewName: " + propertyName;

                if (this.ThrowOnInvalidPropertyName)
                    throw new Exception(msg);
                else
                    Debug.Fail(msg);
            }
        }
        #endregion
        protected virtual bool ThrowOnInvalidPropertyName { get; private set; }

        #region INotifyPropertyChanged Members
        [field: NonSerialized]
        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged(string propertyName)
        {
            this.VerifyPropertyName(propertyName);
            PropertyChangedEventHandler handler = this.PropertyChanged;
            if (handler != null)
            {
                var e = new PropertyChangedEventArgs(propertyName);
                handler(this, e);
            }
        }
        #endregion
    }
}
